package com.wzt.lianfirecontrol.adapter.xuncha.devicexuncha;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.function.xuncha.devicexuncha.XunChaDeviceFloorModel;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.Utils;

public class XunChaDeviceSelectFloorAdapter extends BaseRecyclerAdapter<XunChaDeviceFloorModel> {

    private Context context;

    private boolean isHistory = false;

    private String floorId;

    public String getFloorId() {
        return floorId;
    }

    public void setFloorId(String floorId) {
        this.floorId = floorId;
    }

    public boolean isHistory() {
        return isHistory;
    }

    public void setHistory(boolean history) {
        isHistory = history;
    }

    public XunChaDeviceSelectFloorAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_xuncha_select_floor, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, XunChaDeviceFloorModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{

        private LinearLayout ll_item_content;
        private TextView tv_floor_name;
        private TextView tv_name;
        private TextView tv_status;
        private ImageView iv_floor_select;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.tv_floor_name = itemView.findViewById(R.id.tv_floor_name);
        myViewHolder.tv_name = itemView.findViewById(R.id.tv_name);
        myViewHolder.tv_status = itemView.findViewById(R.id.tv_status);
        myViewHolder.iv_floor_select = itemView.findViewById(R.id.iv_floor_select);
    }

    private void setItemData(MyViewHolder viewHolder, XunChaDeviceFloorModel data, int realPosition) {
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();
        if (realPosition == 0) {
            layoutParams.setMargins(0, 0, 0, 0);
        } else {
            layoutParams.setMargins(0, Utils.dip2px(context, 0.5f), 0, 0);
        }
        if (!StringUtils.isEmpty(floorId) && floorId.equals(data.getId())){
            viewHolder.ll_item_content.setBackgroundColor(context.getResources().getColor(R.color.main_color_tran));
        }else {
            if (isHistory) {
                viewHolder.ll_item_content.setBackgroundColor(context.getResources().getColor(R.color.white));
            } else {
                viewHolder.ll_item_content.setBackgroundColor(context.getResources().getColor(R.color.divider_line_color));
            }
        }
        viewHolder.tv_floor_name.setText(data.getFloorName());
        viewHolder.tv_floor_name.setTextColor(context.getResources().getColor(R.color.text_color));
        viewHolder.tv_name.setVisibility(View.INVISIBLE);
        viewHolder.iv_floor_select.setVisibility(View.GONE);

        if (StringUtils.isEmpty(data.getStatus())){
            viewHolder.tv_status.setVisibility(View.GONE);
        }else {
            if ("4".equals(data.getStatus())){
                viewHolder.tv_status.setText("已过期");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.hint_text_color));
            }else if ("1".equals(data.getStatus())){
                viewHolder.tv_status.setText("待检查");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.change_status_color));
            }else if ("2".equals(data.getStatus())){
                viewHolder.tv_status.setText("检查中");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.look_status_color));
            }else if ("3".equals(data.getStatus())){
                viewHolder.tv_status.setText("已完成");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.main_color));
            }
        }

    }
}
